package com.fesine.ssh.service.impl;

import com.fesine.ssh.dao.SequenceDao;
import com.fesine.ssh.model.Sequence;
import com.fesine.ssh.service.SequenceService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Fesine on 2016/2/18.
 */
@Service("sequenceService")
public class SequenceServiceImpl implements SequenceService {
    @Autowired
    SequenceDao sequenceDao;



    @Override
    public String add(Sequence entity) {
        return sequenceDao.save(entity);
    }

    @Override
    public void delete(String id) {
        sequenceDao.delete(id);
    }

    @Override
    public Sequence getSeq(String name) {
        return sequenceDao.getSeq(name);
    }

    /**
     * 查询序列表取出userId当前值,拼接成USR0000000001格式
     * 判断当前值是不是小于最大值,如果是
     * 将下一下值给当前值,下一值加一再给当前值
     * 更新数据库
     *
     * @return
     */
    @Override
    public String genUserId() {
        String userId = "";
        Sequence sequences = sequenceDao.getSeq("genUserId");//得到当前序列表
        if (null != sequences) {
            int currVal = sequences.getCurrVal();//取出当前值
            userId = "USR" + StringUtils.leftPad(currVal + "", 10, "0");//向左补全0
            //更新序列表
            int nextVal = sequences.getNextVal();//取出下一值
            sequences.setCurrVal(nextVal);
            sequences.setNextVal(nextVal + 1);
            sequenceDao.saveOrUpdate(sequences);//执行更新
        } else {
            try {
                throw new Exception("用户id序列查询失败!");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return userId;
    }

    @Override
    public String genOrderId() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String prexStr = sdf.format(new Date());
        String orderId = "";//201602230001
        Sequence sequences = sequenceDao.getSeq("genOrderId");//得到当前序列表
        if (null != sequences) {
            int currVal = sequences.getCurrVal();//取出当前值
            orderId = prexStr + StringUtils.leftPad(currVal + "", 4, "0");//向左补全0
            //更新序列表
            int nextVal = sequences.getNextVal();//取出下一值
            sequences.setCurrVal(nextVal);
            sequences.setNextVal(nextVal + 1);
            sequenceDao.saveOrUpdate(sequences);//执行更新
        } else {
            try {
                throw new Exception("用户id序列查询失败!");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return orderId;
    }


    @Override
    public int genCateId() {
        int cateId=0;
        Sequence sequences = sequenceDao.getSeq("genCateId");//得到当前序列表
        if (null != sequences) {
            int currVal = sequences.getCurrVal();//取出当前值
            cateId = currVal;
            //更新序列表
            int nextVal = sequences.getNextVal();//取出下一值
            sequences.setCurrVal(nextVal);
            sequences.setNextVal(nextVal + 1);
            sequenceDao.saveOrUpdate(sequences);//执行更新
        } else {
            try {
                throw new Exception("商品分类id序列查询失败!");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return cateId;
    }


}
